﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace BLL
{
    public class NewsBll
    {
        DAL.NewsDal nd = new DAL.NewsDal();

        public DataTable GetAllNews(int pageNow)
        {
            return nd.GetNewsAll(pageNow);
        }

        public DataTable GetTopNews()
        {
            return nd.GetTopNews();
        }

        public DataTable GetNewsByCategory(int categoryId,int pageNow)
        {
            return nd.GetNewsByCategory(categoryId,pageNow);
        }

        public bool AddNews(Model.News news)
        {
            return nd.AddNews(news);
        }

        public Model.News GetNewsContent(int id)
        {
            DataTable table = nd.GetNewsContent(id);
            Model.News news = new Model.News();

            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["Id"] != null && table.Rows[0]["Id"].ToString() != "")
                {
                    news.Id = int.Parse(table.Rows[0]["Id"].ToString());
                }
                if (table.Rows[0]["CategoryId"] != null && table.Rows[0]["CategoryId"].ToString() != "")
                {
                    news.CategoryId = int.Parse(table.Rows[0]["CategoryId"].ToString());
                }
                if (table.Rows[0]["Title"] != null && table.Rows[0]["Title"].ToString() != "")
                {
                    news.Title = table.Rows[0]["Title"].ToString();
                }
                if (table.Rows[0]["Content"] != null && table.Rows[0]["Content"].ToString() != "")
                {
                    news.Content = table.Rows[0]["Content"].ToString();
                }
                if (table.Rows[0]["Time"] != null && table.Rows[0]["Time"].ToString() != "")
                {
                    news.Time = table.Rows[0]["Time"].ToString();
                }
                if (table.Rows[0]["Author"] != null && table.Rows[0]["Author"].ToString() != "")
                {
                    news.Author = table.Rows[0]["Author"].ToString();
                }
                if (table.Rows[0]["CategoryName"] != null && table.Rows[0]["CategoryName"].ToString() != "")
                {
                    news.CategoryName = table.Rows[0]["CategoryName"].ToString();
                }

                return news;
            }
            else
            {
                return null;
            }
        }

        public DataTable GetNewsCategory()
        {
            return nd.GetNewsCategory();
        }

        public bool UpdateNews(Model.News news)
        {
            return nd.UpdateNews(news);
        }

        public bool DeleteNewsContent(int id)
        {
            return nd.DeleteNewsContent(id);
        }

        public DataTable GetNewsAll2()
        {
            return nd.GetNewsAll2();
        }

        public DataTable GetNewsByCategory2(int categoryId)
        {
            return nd.GetNewsByCategory2(categoryId);
        }

        public int GetNewsCount()
        {
            return nd.GetNewsAllCount();
        }

        public int GetNewsCountByCategory(int categoryId)
        {
            return nd.GetNewsCountByCategory(categoryId);
        }
    }
}
